(function () {
	var d={};
	$("#avatar").error(function() { 
    	$(this).attr("src", "../img/empty.png"); 
    	$(this).error=null;
    	}); 
	/*jQuery("#avatar").bind("click",function(){
		PictureUploadBusiness();
	});*/
	$("#file").change(function () {
	    var $file = $(this);
	    var fileObj = $file[0];
	    var windowURL = window.URL || window.webkitURL;
	    var dataURL;
	    var $img = $("#avatar");

	    if (fileObj && fileObj.files && fileObj.files[0]) {
	        dataURL = windowURL.createObjectURL(fileObj.files[0]);
	        $img.attr('src', dataURL);
	    } else {
	        dataURL = $file.val();
	        var imgObj = document.getElementById("preview");
	        // 两个坑:
	        // 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
	        // 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
	        imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
	        imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

	    }
	});
	jQuery('#fileclick').on('click', function () {
		return  $("#file").click();
	});
	/**
	 * 图片上传
	*/
	PictureUploadBusiness = function()
	{
	     var root=window.rootPath;	   
	     WebFilesUploader(
		           {
			        _webuploader:{
			            server: root + '/a/'+d.id+'/avatar',
			        	chunked:false
			        }
			    },
			    function(jsons)
			    {
			    	var avatarUrl=window.rootPath+'/a/'+'avatar/'+d.id;
			    	jQuery("#avatar").prop("src","#");
					jQuery("#avatar").prop("src",avatarUrl+'?t='+Math.random());
			    });
	};
	$(document).ready(function() {
		/*$("#buInfoEdit").attr("href",$("#goHome").attr("href")+"&&identify="+$('#identify').val());*/
		$("#userAccount").html($("#userName").val());
		showWxCode();
	});
	function showWxCode(){
		var registerOpenid =$("#registerOpenid").val();
		if(registerOpenid==undefined||registerOpenid=="")
			{
			$("#wxInfoEdit").html("立即绑定"); 
			$("#wxInfo").html("你还未绑定微信，绑定微信后可以直接通过微信扫码登录"); 
			}else
				{
				$("#wxInfoEdit").html("取消绑定"); 
				$("#wxInfo").html("你已经绑定微信，可以直接通过微信扫码登录"); 
				}
	}
    ///编辑商户信息
	$("#buInfoEdit").on('click', function () {
		jQuery('#checkModal').modal('show');
		getData();
	});
	 ///修改密码
	$("#pwdInfoEdit").on('click', function () {
		jQuery('#pwdModal').modal('show');
	});
	 ///设置微信绑定
	$("#wxInfoEdit").on('click', function () {
		var code ="";
		var str ="请拿出手机，扫描屏幕右侧二维码立即进行绑定！";
		if($("#wxInfoEdit").html()=="立即绑定"){
			$.springLemonDialog({
				content : str,// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
					if(index==0)
					{
						var codeUrl =window.rootPath+$("#codeAdress").val();
						 codeUrl=window.location.origin+codeUrl;
						 
						 $('#qrcode').html("");
						/*jQuery('#qrcode').qrcode(codeUrl);*/
						
						var qrcode =jQuery('#qrcode').qrcode({
						    render: "canvas", //也可以替换为table
						    width: 300,
						    height: 300,
						    text:codeUrl
						}).hide();
					 var canvas=qrcode.find('canvas').get(0);
						
					 $('#dimg').attr('src',canvas.toDataURL('dimg/png'));
					}
					else{
						
					}
				}
			});
		}
		else
		{
			code="";
			str='确认取消微信绑定？';
		
			$.springLemonDialog({
				content : str,// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
					if(index==0)
					{
						d2={
							"wxCode":code	
						}
						var url =window.rootPath
						+ '/ui/userset/setWxCode';
						jQuery.ajax({
			               	type : "post",
			               	url: encodeURI(url),            	
			               	dataType : 'json', 
			               	data: JSON.stringify(d2),
			               	contentType: 'application/json;charset=utf-8',
			               	success : function(data) {
			               		if (data.code == -1) {
									message("操作失败")
								} else {
									$("#registerOpenid").val("");
									showWxCode();
								}
			               	}
			        	});
					}
					else{
						
					}
				}
			});
		}
		
		
	});
	
	//重新设置密码
	jQuery('.savepwdBtn').on('click', function () {
    	
	    var url = window.rootPath +'/ui/userset/updatePwd';
	    
	    	d1={
	    			"opwd":sha256_digest($("#opwd").val()),
	    			"npwd":sha256_digest($("#npwd").val())
	    	}
	    	
	    	jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d1),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message(data.message);
	            	}
	            	else{
	            		
	            	}
	            	$("#opwd").val("");
	            	$("#npwd").val("");
	            	jQuery('#pwdModal').modal('hide');
	            }
	        });
        
});
	
    jQuery('.saveBtn').on('click', function () {
    	
		    var url = window.rootPath +'/ui/bussinessEdit/saveDetailWithFile';
		    
		    	d.shopName=$("#shopName").val();
		    	d.shopAdress=$("#shopAdress").val();
		    	d.areaMeasure=$("#areaMeasure").val();
		    	d.signPerson=$("#signPerson").val();
		    	d.cleaningPerson=$("#cleaningPerson").val();
		    	d.managementRange=$("#managementRange").val();
		    	
		    	
		    	if(d.areaInfo!=undefined){
		    		d.areaInfo.id=$('#area').val();
		    	} else{
		    		d.areaInfo={"id":$('#area').val()};	
		    	}
		    	
		    	if(d.arreaManagementPerson!=undefined){
		    		d.arreaManagementPerson.id=$('#areaMperson').val();
		    	}else{
		    		d.arreaManagementPerson={"id":$('#areaMperson').val()};
		    	}
		    	
		    	var data={
		    			"id":d.id,
		    			"shopName":d.shopName,
		    			"shopAdress":d.shopAdress,
		    			"areaMeasure":d.areaMeasure,
		    			"signPerson":d.signPerson,
		    			"cleaningPerson":d.cleaningPerson,
		    			"infoState":d.infoState.id,
		    			"longitude":d.longitude,
		    			"latitude":d.latitude,
		    			"managementRange":d.managementRange,
		    			"areaInfo":d.areaInfo.id,//JSON.stringify({"id":d.areaInfo.id}),
		    			"arreaManagementPerson":d.arreaManagementPerson.id,//JSON.stringify({"id":d.arreaManagementPerson.id}),
		    		    "businesssellerBaseInfo":d.businesssellerBaseInfo.id//JSON.stringify({"id":d.businesssellerBaseInfo.id})
		    	}
		    	
		    	/*d.areaInfo.id=$('#area').val();
		    	d.arreaManagementPerson.id=$('#areaMperson').val();*/
		    	
		    	/*jQuery.ajax({
		            type: 'post',
		            url: encodeURI(url),
		            data: JSON.stringify(d),
		            dataType: 'json',
		            contentType: 'application/json;charset=utf-8',
		            success: function (data) {
		            	if(data.code==-1){
		            		message(data.message);
		            	}
		            	else{
		            		
		            		$("#popupView").popup();
		            		$("#popupView").popup('open');
		            		$("#test").trigger("click"); 
		            	}
		            	jQuery('#checkModal').modal('hide');
		            	getData();
		            }
		        });*/
		    	
		    	$.ajaxFileUpload({
		    		url : url,
		    		secureuri : false,// 是否需要安全协议
		    		fileElementId : ["file"],// 文件上传的Id属性
		    		dataType : 'text',
		    		data : data,
		    		success : function(result, status) {
		    			if (status=="success") {
		    			}else{
		    				message(data.message);
		    			}
		    			jQuery('#checkModal').modal('hide');
		            	getData();
		    			
		    		}
		    	});
	        
    });
    
    //提交
    jQuery('.submitBtn').on('click', function () {
	    var url = window.rootPath +'/ui/bussinessEdit/submitDetailWithFile';

	    	d.shopName=$("#shopName").val();
	    	d.shopAdress=$("#shopAdress").val();
	    	d.areaMeasure=$("#areaMeasure").val();
	    	d.signPerson=$("#signPerson").val();
	    	d.cleaningPerson=$("#cleaningPerson").val();
	    	d.managementRange=$("#managementRange").val();
	    	
	    	if(d.areaInfo!=undefined){
	    		d.areaInfo.id=$('#area').val();
	    	} else{
	    		d.areaInfo={"id":$('#area').val()};	
	    	}
	    	
	    	if(d.arreaManagementPerson!=undefined){
	    		d.arreaManagementPerson.id=$('#areaMperson').val();
	    	}else{
	    		d.arreaManagementPerson={"id":$('#areaMperson').val()};
	    	}
	    	
	    	/*d.areaInfo.id=$('#area').val();
	    	d.arreaManagementPerson.id=$('#areaMperson').val();*/
	    	var data={
	    			"id":d.id,
	    			"shopName":d.shopName,
	    			"shopAdress":d.shopAdress,
	    			"areaMeasure":d.areaMeasure,
	    			"signPerson":d.signPerson,
	    			"cleaningPerson":d.cleaningPerson,
	    			"infoState":d.infoState.id,
	    			"longitude":d.longitude,
	    			"latitude":d.latitude,
	    			"managementRange":d.managementRange,
	    			"areaInfo":d.areaInfo.id,//JSON.stringify({"id":d.areaInfo.id}),
	    			"arreaManagementPerson":d.arreaManagementPerson.id,//JSON.stringify({"id":d.arreaManagementPerson.id}),
	    		    "businesssellerBaseInfo":d.businesssellerBaseInfo.id//JSON.stringify({"id":d.businesssellerBaseInfo.id})
	    	}
	    	
	    	/*jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            	if(data.code==-1){
	            		message(data.message);
	            	}
	            	jQuery('#checkModal').modal('hide');
	            	getData();
	            }
	    	
	        });*/
	    	$.ajaxFileUpload({
	    		url : url,
	    		secureuri : false,// 是否需要安全协议
	    		fileElementId : ["file"],// 文件上传的Id属性
	    		dataType : 'text',
	    		data : data,
	    		success : function(result, status) {
	    			if (status=="success") {
	    				jQuery('#checkModal').modal('hide');
	    				getData();
	    			}
	    			else{
	    				message(data.message);
	    			}
	    		}
	    	});
});
    function getData(){
    	var identify =$("#identify").val();
        var url = window.rootPath +'/ui/bussinessEdit/getEditDetail/'+identify;
        jQuery.ajax({
            url:url,
            cache:false,
            type :"get",
            data:"",
            dataType:"json",
            contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
            success:function(data){
            	d=data;
            	jQuery('#infoState').prop('readonly', 'readonly');
            	jQuery('#managementPerson').prop('readonly', 'readonly');
            	
            	if(data.infoState.title=="待认证")
            	{
            		jQuery('#cleaningPerson').prop('readonly', 'readonly');
            		jQuery('#managementRange').prop('readonly', 'readonly');
            		jQuery('#signPerson').prop('readonly', 'readonly');
            		jQuery('#shopAdress').prop('readonly', 'readonly');
            		jQuery('#pictureAdress').prop('readonly', 'readonly');
            		$('#area').attr('disabled','disabled');
            		$('#areaMperson').attr('disabled','disabled');
            		jQuery(".submitBtn").attr("disabled", true);
            		}
            	
            	/*var avatarUrl=window.rootPath+'/a/'+'avatar/'+data.pictureAdress;
        		jQuery("#avatar").prop("src",avatarUrl+'?t='+Math.random());
        		*/
        		var purl=data.pictureAdress;
				if(purl==undefined||purl==""){
					purl="../img/nopicture.png";
				}else{
					purl=window.rootPath+'/'+purl;
				}
				$("#avatar").attr("src",purl);
				
            	$("#shopName").val(data.shopName);
            	
            	var path ="";
        		if (data.infoState.title == '待认证') {
        			path="../img/authentication_in.png";
				} else if (data.infoState.title == '认证拒绝') {
					path="../img/authentication_notaccess.png";
				} else if (data.infoState.title == '认证通过') {
					path="../img/authentication_access.png";
				} else if (data.infoState.title == '初始化') {
					path="../img/authentication_init.png";
				} else if(data.infoState.title	==null){
					path ="";
				}
        		$("#infoState").attr('src',path);
            	/*$("#infoState").val(data.infoState.title);*/
            	
            	$("#shopAdress").val(data.shopAdress);
            	$("#areaMeasure").val(data.areaMeasure);
            	$("#signPerson").val(data.signPerson);
            	$("#cleaningPerson").val(data.cleaningPerson);
            	$("#managementRange").val(data.managementRange);
            	$("#managementPerson").val(data.managementPerson.name);
            	
            	if(data.areaInfo!=undefined){
            	jQuery('#area').val(data.areaInfo.id);
            	}
            	if(data.arreaManagementPerson!=undefined){
            		jQuery('#areaMperson').val(data.arreaManagementPerson.id);
            	}
            	//$('#area').val(data.areaInfo.id).selectmenu('refresh', true); 
            	//$('#areaMperson').val(data.arreaManagementPerson.id).selectmenu('refresh', true); 
            	//$("#area").find("option:contains('"+data.areaInfo.title+"')").attr("selected",true);
            	//jQuery('#area').val(data.areaInfo.id);
            	//$("#areaMperson").find("option:contains('"+data.arreaManagementPerson.name+"')").attr("selected",true);
            	//jQuery('#areaMperson').val(data.arreaManagementPerson.id);
            	
            	/*$("#managementPerson").html("管理人员: "+data.managementPerson.name); */
            }
        })
    };
})();